import 'package:sui_shou_pai/chat/entity/global.dart';
import 'package:sui_shou_pai/chat/init/init_map.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';
import 'package:sui_shou_pai/record/record_entity/photo_record_entity.dart';
import 'package:sui_shou_pai/utils/aes_util.dart';
import 'package:sui_shou_pai/utils/storage_util.dart';

class ChatInterface {
  /**
   * 发送信息
   */
  static Future<void> addRecord(String content, String receiver) async {
    // BaseOptions options =
    //     new BaseOptions(baseUrl: BASE_URL, connectTimeout: 50000);
    // Dio dio = new Dio(options);
    // var response = await dio.post("/chat-record/saveOne", data: {
    //   "createTime": Global.currentTimeMillis(),
    //   "updateTime": Global.currentTimeMillis(),
    //   "time": Global.currentTimeMillis(),
    //   "senderId": "9",
    //   "receiverId": receiver,
    //   "content": content,
    //   "isRead": "0"
    // });

    HttpRequest.request("$BASE_URL/chat-record/saveOne",
            method: "post",
            data: {
              "createTime": Global.currentTimeMillis(),
              "updateTime": Global.currentTimeMillis(),
              "time": Global.currentTimeMillis(),
              "senderId": "9",
              "receiverId": receiver,
              "content": content,
              "isRead": "0"
            },
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      String result = value.data["message"];
      if (result == "发送成功") {
        print("发送成功");
      } else {
        print("发送失败");
      }
    });
  }

  static Future<void> addBossRecord(String content, String receiver) async {
    String employeeId = await StorageUtil.getStringItem("employeeId");
    // BaseOptions options =
    //     new BaseOptions(baseUrl: BASE_URL, connectTimeout: 50000);
    // Dio dio = new Dio(options);
    // var response = await dio.post(
    //   "/chat-record/saveOne",
    //   data: {
    //     "createTime": Global.currentTimeMillis(),
    //     "updateTime": Global.currentTimeMillis(),
    //     "time": Global.currentTimeMillis(),
    //     "senderId": employeeId,
    //     "receiverId": receiver,
    //     "content": content,
    //     "isRead": "0"
    //   },
    // );

    HttpRequest.request("$BASE_URL/chat-record/saveOne",
            method: "post",
            data: {
              "createTime": Global.currentTimeMillis(),
              "updateTime": Global.currentTimeMillis(),
              "time": Global.currentTimeMillis(),
              "senderId": employeeId,
              "receiverId": receiver,
              "content": content,
              "isRead": "0"
            },
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      String result = value.data["message"];
      if (result == "发送成功") {
        print("发送成功");
      } else {
        print("发送失败");
      }
    });
  }

  /**
   * 修改系统消息为已读
   */
  static Future<void> changeSystemIsRead(String recordId) async {
    HttpRequest.request("$BASE_URL/chat-record/updateSystemIsread",
            method: "get",
            params: {
              "recordId": recordId,
            },
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        print("修改系统消息为已读成功");
      } else {
        print("修改系统消息为已读失败");
      }
    });
  }

  /**
   * 点击好友记录，将该好友消息修改为已读
   */
  static Future<void> changeIsread(String otherId) async {
    HttpRequest.request("/chat-record/updateIsread",
            method: "get",
            params: {
              "me": AesUtil.aesEncrypt(Global.me),
              "other": AesUtil.aesEncrypt(otherId)
            },
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        print("修改好友消息为已读成功");
      } else {
        print("修改好友消息为已读失败");
      }
    });
    InitMap.messageMap[otherId].setNum("0");
  }

  /**
   * 根据id获取照片记录实体
   */
  static Future<void> getPhotoRecord(String photoId) async {
    print("----chat_recards_list---photoId--" + photoId);
    HttpRequest.request("/photo-record/queryById",
            method: "get",
            params: {
              "id": photoId,
            },
            token: await StorageUtil.getStringItem("token"))
        .then((value) {
      if (value.data["code"] == 200) {
        print("获取图片实体成功");
        print(value.data["result"].toString());
        PhotoRecordEntity photoRecordEntity =
            PhotoRecordEntity.fromJson(value.data["result"]);
        photoRecordEntity.status="4";
        Global.photoRecordEntity = photoRecordEntity;
      } else {
        print("获取图片实体失败");
      }
    });
  }
}
